export function getBrowserClientInfo() {
	if (typeof navigator !== 'undefined') {
		var Sys = {};
		var ua = navigator.userAgent.toLowerCase();
		var re = /(msie|firefox|chrome|opera|version).*?([\d.]+)/;
		var m = ua.match(re);
		try {
			Sys.type = m[1].replace(/version/, 'safari');
			Sys.version = m[2];
		} catch (e) {
			Sys.type = 'unknown';
			Sys.version = 'unknown';
		}
		return Sys;
	} else {
		return {
			type: 'robot',
			version: '0.1',
		};
	}
}

export function removeObjectWithId(sourceData, removeObjectId) {
	try {
		for (let i = 0; i < sourceData.length; i++) {
			if (sourceData[i].id == removeObjectId) {
				sourceData.splice(i, 1);
				return sourceData;
			}
		}
	} catch (e) {
		return sourceData;
	}
}

export function deepCopy(obj) {
	var o;
	switch (typeof obj) {
		case 'undefined':
			break;
		case 'string':
			o = obj + '';
			break;
		case 'number':
			o = obj - 0;
			break;
		case 'boolean':
			o = obj;
			break;
		case 'object':
			if (obj === null) {
				o = null;
			} else {
				if (obj instanceof Array) {
					o = [];
					for (var i = 0, len = obj.length; i < len; i++) {
						o.push(deepCopy(obj[i]));
					}
				} else {
					o = {};
					for (var k in obj) {
						o[k] = deepCopy(obj[k]);
					}
				}
			}
			break;
		default:
			o = obj;
			break;
	}
	return o;
}

export function isMobileBrowser() {
	if (window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
		return true;
	} else {
		return false;
	}
}
/**
 * 是否是JSON字符串，支持JSON.stringify
 * @param {*} str
 */
export function isJsonString(str) {
	if (typeof str == 'string') {
		try {
			JSON.parse(str);
			return true;
		} catch (e) {
			return false;
		}
	} else {
		return false;
	}
}

/**
 * 是否是空对象
 * @param {*} object
 */
export function isNullObject(object) {
	if (object == null || typeof object != 'object') {
		return true;
	}
	return false;
}

/**
 * 是否是空字符串
 * @param {*} str
 */
export function isEmptyString(str) {
	if (typeof str == 'string') {
		if (str != null && str != undefined && str.length != 0) {
			return false;
		}
	}
	return true;
}

/**
 * 判断某一个字符串是否以某个字符结尾
 * @param {string} str 原字符串
 * @param {string} appoint 结尾字符串
 */
export function validationStringEnd(str, appoint) {
	str = str.toLowerCase();
	var start = str.length - appoint.length;
	var char = str.substr(start, appoint.length);
	if (char == appoint) {
		return true;
	}
	return false;
}

/**
 * 获取一个打印的对象的值
 * @param {string} o
 * @returns {string} 打印的内容
 */
export function logObject(o) {
	console.log('logObject',o);
	try {
		var temp = '';
		var t,
			a = [];
		for (var i in o) {
			try {
				if (typeof o[i] == 'function') {
					t = '""' + i + '""' + ':' + 'function(){<em>[native code]</em>}' + '';
				} else if (Object.prototype.toString.call(o[i]) === '[object Array]') {
					//数组
					var p,
						b = [];
					for (var j in o[i]) {
						if (isNaN(o[i][j])) {
							p = '"' + o[i][j] + '"';
						} else {
							p = '' + o[i][j] + '';
						}
						b.push(p);
					}
					t = '""' + i + '""' + ':[' + b.join(',') + ']';
				} else {
					if (typeof o[i] == 'object') {
						//对象,其他
						t = '""' + i + '""' + ':' + logObject(o[i]) + '';
					} else {
						if (isNaN(o[i])) {
							t = '""' + i + '""' + ':"' + o[i] + '"';
						} else {
							t = '""' + i + '""' + ':' + o[i] + '';
						}
					}
				}
				a.push(t);
				t = '';
			} catch (e) {
				return '';
			}
		}
		temp = '{' + a.join(', ') + '}';
		return temp;
	} catch (error) {
		console.log('logObject error',error);
	}

}
/**
 * 上传logs日志去后台
 * @param  {...any} options
 */
import { uploadlogs } from './api';
export function uploadLogsToFile(...options) {
	return new Promise((resolve, reject) => {
		let url = options && options[0];
		let sessionId = options && options[1];
		let data = options && options[2];
		uploadlogs(url, sessionId, data)
			.then(resp => {
				resolve(resp);
			})
			.catch(error => {
				reject(error);
			});
	});
}

/**
 * 留痕相关错误信息
 * @param {string|object} error
 * @param {Array} options
 */
export async function errorlogs(error, ...options) {
	if (typeof error === 'string') {
		let msg = `ivcs-js-sdk-debug:${new Date().toISOString()},errorMsg:${error}`;
		let url = options && options[0] && options[0].url ? options[0].url : undefined;
		let sessionId = options && options.length > 1 ? options[1] : undefined;
		url && sessionId && (await uploadLogsToFile(url, sessionId, msg));
	} else if (isJsonString(error)) {
		let msg = `ivcs-js-sdk-debug:${new Date().toISOString()},json,errorMsg:${JSON.stringify(error)}`;
		let url = options && options[0] && options[0].url ? options[0].url : undefined;
		let sessionId = options && options.length > 1 ? options[1] : undefined;
		url && sessionId && (await uploadLogsToFile(url, sessionId, msg));
	} else if (typeof error === 'object') {
		let url = options && options[0] && options[0].url ? options[0].url : undefined;
		let sessionId = options && options.length > 1 ? options[1] : undefined;
		url && sessionId && (await uploadLogsToFile(url, sessionId, error));
	}
}

/**
 * 判断当前浏览器是否支持webrtc
 * @returns boolean
 */
export function isSupportWebrtc() {
	var isRtcPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection,
		isUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia || navigator.mozGetUserMedia || (navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
		isWebSocket = window.WebSocket;
	return !!isRtcPeerConnection && !!isUserMedia && !!isWebSocket;
}

/**
 * 参数是否为空
 * @param {string|object} param 
 * @returns 
 */
export function paramEmpty(param) {
	if (param === undefined || param === null || param === '') {
		return true; // 有参数为空则返回true
	}
	return false; // 所有参数都有值
}